package ch10_thread.thread;

/**
 * 没有sleep和wait方法时停止线程
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class StopInterrupt implements Runnable {
    @Override
    public void run() {
        int num = 0;
        while (num <= Integer.MAX_VALUE / 2) {
            // 不能省略 !Thread.currentThread().isInterrupted()
            if (num % 10000 == 0 && !Thread.currentThread().isInterrupted()) {
                System.out.println(num + "是10000的倍数");
            }
            num++;
        }
        System.out.println("执行结束...");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopInterrupt());
        thread.start();
        Thread.sleep(500);
        thread.interrupt();
    }
}
